package com.maker.LMMS.service;

import com.blade.jdbc.page.Page;
import com.blade.mvc.http.Request;
import com.maker.LMMS.model.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : zimu
 * @Date: 2018/8/3 12:07
 * @description : 显示信息实现类
 */
public class AdminShowImpl {

    public void showMember(Request request, String key, int page, String orderby){
        Users users = new Users();
        Group group = new Group();
        Page<Users>  usersList = users.like("m_realname","%"+key+"%").and("m_flag",1).page(page,10,orderby);
        List groups = new ArrayList();
        for (Users user : usersList.getRows()) {
            Group g = group.query("SELECT * FROM `group` WHERE g_id=?",user.getM_group());
            if (g==null){
                g = new Group();
                g.setG_gname("null");
            }
            groups.add(g);
        }
        request.attribute("groups", groups);
        request.attribute("users", usersList);
        request.attribute("search", key);
        request.attribute("page", page);
        request.attribute("orderby", orderby);
    }


    public void showAdmin(Request request,String name,int page){
        Admin admins = new Admin();
        Page<Admin>  usersList = admins.like("a_username","%"+name+"%").page(page,10,"a_id");
        request.attribute("admins", usersList);
        request.attribute("search", name);
        request.attribute("page", page);
    }

    public void showGroup(Request request, String key, int page){
        Group groups = new Group();
        Users users = new Users();
        Page<Group>  groupsList = groups.like("g_gname","%"+key+"%").page(page,10,"g_id");
        List member = new ArrayList();
        for (Group group : groupsList.getRows()) {
            Users u = users.query("SELECT * FROM `member` WHERE m_id=?",group.getG_leader());
            if (u==null)
            {
                u = new Users();
                u.setM_realname("null");
            }
            member.add(u);
        }
        request.attribute("groups", groupsList);
        request.attribute("member", member);
        request.attribute("search", key);
        request.attribute("page", page);
    }

    public void showDevelopment(Request request, String key, int page){
        Development developments = new Development();
        Page<Development>  usersList = developments.like("d_title","%"+key+"%").page(page,10,"d_id desc");
        request.attribute("developments", usersList);
        request.attribute("search", key);
        request.attribute("page", page);
    }

    public void showAboutus(Request request){
        Aboutus aboutus = new Aboutus();
        aboutus = aboutus.query("SELECT * FROM `aboutus` where ab_id=1");
        request.attribute("aboutus", aboutus);
    }

    public void showFeedback(Request request,int flag,String con,int page){
        Feedback feedbacks = new Feedback();
        Users member = new Users();
        Page<Feedback>  usersList = feedbacks.like("f_content","%"+con+"%").and("f_flag",flag).page(page,10,"f_id desc");
        List members = new ArrayList();
        for (Feedback user : usersList.getRows()) {
            Users u = member.query("SELECT * FROM `member` WHERE m_id=?",user.getF_menberid());
            if (u==null)
            {
                u = new Users();
                u.setM_realname("null");
                u.setM_id(user.getF_menberid());
            }
            u.setM_password("");
            members.add(u);
        }
        request.attribute("feedbacks", usersList);
        request.attribute("members", members);
        request.attribute("search", con);
        request.attribute("page", page);
    }


    public void showEditRole(Request request, Integer id) {
        Admin admin = new Admin();
        admin.setA_id(id);
        admin = admin.find();
        request.attribute("admin", admin);
    }

    public void feedbackDetail(int id, Request request) {
        Feedback feedback = new Feedback();
        Users member =new Users();
        feedback.setF_id(id);
        feedback = feedback.find();
        member.setM_id(feedback.getF_menberid());
        member = member.find();
        if (member==null){
            member = new Users();
            member.setM_studentNumber("0");
            member.setM_realname("null");
        }
        request.attribute("feedback", feedback);
        request.attribute("member", member);

        Feedback feedback_2 = new Feedback();
        feedback_2.setF_flag(1);
        feedback_2.update("f_id",id);
    }

    public void showAudit(Request request, String search, int page) {
        Users users = new Users();
        Page<Users> list = users.like("m_realname","%"+search+"%").and("m_flag",0).page(page,10,"m_id");
        request.attribute("audits", list);
        request.attribute("search", search);
        request.attribute("page", page);
    }

    public void showAudit2(Request request, String con,int page) {
        Checklog users = new Checklog();
        Page<Checklog> list = users.like("c_member","%"+con+"%").page(page,10,"c_id desc");
        request.attribute("audits", list);
        request.attribute("search", con);
        request.attribute("page", page);
    }

    public void showRegSetting(Request request) {
        Resigter resigter = new Resigter();
        resigter.setR_id(1);
        resigter = resigter.find();
        request.attribute("resigter", resigter);
    }

    public void showEditHistory(Request request, int id) {
        Development development = new Development();
        development.setD_id(id);
        development = development.find();
        request.attribute("development", development);
    }

    public void showEditGroup(Request request, int id) {
        Group group = new Group();
        group.setG_id(id);
        group = group.find();
        request.attribute("group", group);
    }

    public void showEditUser(Request request, int id) {
        Users users = new Users();
        users.setM_id(id);
        users = users.find();
        Group mygroup = new Group();
        mygroup.setG_id(users.getM_group());
        mygroup = mygroup.find();
        if (mygroup==null){
            mygroup = new Group();
            mygroup.setG_gname("null");
        }
        Group group = new Group();
        List<Group> groups = group.findAll();
        request.attribute("member", users);
        request.attribute("mygroup", mygroup);
        request.attribute("groups", groups);
    }

    public void allGroup(Request request) {
        Group group = new Group();
        List<Group> groups = group.findAll();
        request.attribute("groups", groups);
    }

    public void showGleader(Request request, int page, int id) {
        Leader leader = new Leader();
        Users users = new Users();
        Group group = new Group();
        List<Group> groups = group.findAll();
        Page<Leader>  leaderList = leader.like("le_groupid",id).page(page,10,"le_time desc");
        List member = new ArrayList();
        for (Leader leader1 : leaderList.getRows()) {
            Users u = users.query("SELECT * FROM `member` WHERE m_id=?",leader1.getLe_memberid());
            if (u==null)
            {
                u = new Users();
                u.setM_realname("null");
            }
            u.setM_password("");
            member.add(u);
        }
        group.setG_id(id);
        group = group.find();
        request.attribute("leaders", leaderList);
        request.attribute("member", member);
        request.attribute("page", page);
        request.attribute("group", group);
        request.attribute("groups", groups);
    }

    public void showEditGleader(Request request, int id) {
        Leader leader = new Leader();
        leader.setLe_id(id);
        leader = leader.find();
        request.attribute("leader", leader);
    }
}
